package cn.kgc.bdqn_studentinfomanager.service.AdminService.impl;

import cn.hutool.core.util.ReflectUtil;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.ReturnManagerInfoVo;
import cn.kgc.bdqn_studentinfomanager.service.AdminService.AdminService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * by Shanshui CC on 2020/12/21 10:30
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Override
    public void printChannelManagerData(List<ReturnManagerInfoVo> returnManagerInfoVoList, HttpServletResponse response)  {
        //           获取集合数据
        SimpleDateFormat df = new SimpleDateFormat("yy年MM月");
        String moth = df.format(new Date());
        response.setContentType("application/force-download");
        String fileName = moth+"主管信息.xlsx";

        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(fileName);
        response.addHeader("Content-Disposition", "attachment;fileName="+ fileName);
        //文件流输出
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        try {
            HSSFSheet sheet = workbook.createSheet(moth + "数据");
            //表头
            String[] value =new String[]{"姓名", "区域", "信息量", "报名量"};
            //创建标题 第一行
            HSSFRow title = sheet.createRow(0);
            title.createCell(0).setCellValue("主管信息表");
            //合并单元格 横向4格
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
            //创建表头 第二行
            HSSFRow header = sheet.createRow(1);
            for (int j = 0; j < value.length; j++) {
                header.createCell(j).setCellValue(value[j]);
            }
            for (int i = 0; i < returnManagerInfoVoList.size(); i++) {
                //添加主管信息 第三行开始 循环指定对象内主管数据
                HSSFRow row = sheet.createRow(i + 2);
                //反射遍历字段值
                Field[] fields = ReflectUtil.getFields(returnManagerInfoVoList.get(i).getClass());
                for (int j = 2; j < fields.length; j++) {
                    //从第二字段开始
                    row.createCell(j-2).setCellValue(ReflectUtil.getFieldValue(returnManagerInfoVoList.get(i),fields[j]).toString());
                }
            }
            //打印
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
